package cn.yudao.pet.service.impl;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.basic.utils.BaiduAuditUtils;
import cn.yudao.basic.utils.BaiduImgUtils;
import cn.yudao.pet.domain.Pet;
import cn.yudao.pet.domain.PetDetail;
import cn.yudao.pet.domain.PetOnlineAuditLog;
import cn.yudao.pet.domain.PetType;
import cn.yudao.pet.mapper.PetDetailMapper;
import cn.yudao.pet.mapper.PetMapper;
import cn.yudao.pet.mapper.PetOnlineAuditLogMapper;
import cn.yudao.pet.mapper.PetTypeMapper;
import cn.yudao.pet.service.IPetService;
import cn.yudao.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;

import org.springframework.web.multipart.MultipartFile;
import org.apache.http.entity.ContentType;

import java.io.FileInputStream;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yudao
 * @since 2022-07-16
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {

    @Autowired
    private PetMapper petMapper;

    @Autowired
    private PetDetailMapper petDetailMapper;

    @Autowired
    private PetTypeMapper petTypeMapper;

    @Autowired
    private PetOnlineAuditLogMapper petOnlineAuditLogMapper;

    @Override
    public void save(Pet pet) {
        petMapper.save(pet);
        // 判断是否合规
        if (BaiduAuditUtils.TextCensor(pet.getName())){

        }
        PetDetail detail = pet.getDetail();
        detail.setPetId(pet.getId());
        petDetailMapper.save(detail);
    }

    /**
    * @Title: upadte
    * @Description: 修改宠物表信息
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 20:52
    * @Parameters:
    * @Return
    */
    @Override
    public void upadte(Pet pet) {
        petMapper.upadte(pet);
        // 调用方法修改pet相关表
        updatePetRelated(pet);
        super.upadte(pet);
    }

    /**
    * @Title: updatePetRelated
    * @Description: 修改pet相关表
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 20:57
    * @Parameters:
    * @Return
    */
    public void updatePetRelated(Pet pet){

        PetType petType = pet.getPetType();
        PetDetail detail = pet.getDetail();
        // 修改pet的类型
        petTypeMapper.updateTypeName(petType);
        // 修改pet的详情
        petDetailMapper.upadte(detail);
    }

    @Override
    public void removeById(Long id) {
        if (id != null){
            petMapper.removeById(id);
            petDetailMapper.removePet(id);
        }
    }

    /**
    * @Title: loadDetailByPetId
    * @Description: 获取宠物详情通过宠物的id
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 20:41
    * @Parameters:
    * @Return
    */
    @Override
    public PetDetail loadDetailByPetId(Long id) {
        return petMapper.loadDetailByPetId(id);
    }

    /**
    * @Title: onSalePet
    * @Description: 批量上架
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 21:15
    * @Parameters:
    * @Return
    */
    @Override
    public void onSalePet(Long[] ids) {
        // 查询数据库中所有想要上架的其中还没有上架的商品信息 进行百度ai校验
        List<Pet> petlist = petMapper.loadOnSalePet(ids);
        for (Pet pet : petlist) {
            if (!BaiduAuditUtils.TextCensor(pet.getName())){
                throw new BusinessException("文字违规");
            }
            String resources = pet.getResources();
            if (!StringUtils.isEmpty(resources)){

                String fileUrl = "http://123.207.27.208" + pet.getResources();
                File file = new File(fileUrl);
                FileInputStream fileInputStream = null;
                try {
                    fileInputStream = new FileInputStream(file);
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                try {
                    MultipartFile multipartFile = new MockMultipartFile(file.getName(), file.getName(),
                            ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
                    Boolean aBoolean = BaiduImgUtils.ImgCensor(multipartFile);
                    if (aBoolean){
                        PetOnlineAuditLog petOnlineAuditLog = new PetOnlineAuditLog();
                        petOnlineAuditLog.setPetId(pet.getId());
                        petOnlineAuditLog.setState(0);
                        petOnlineAuditLog.setNote("图片违规:" + fileUrl);
                        petOnlineAuditLogMapper.save(petOnlineAuditLog);
                        throw new BusinessException("图片违规");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        // 批量新增
        petMapper.onSalePet(ids);

    }

    /**
    * @Title: offSalePet
    * @Description: 批量下架
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/16 21:16
    * @Parameters:
    * @Return
    */
    @Override
    public void offSalePet(Long[] ids) {
        petMapper.offSalePet(ids);
    }
}
